%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% File:               replication_best_responses.m
%
% Author:             Sergio Ascencio and Miguel R. Rueda
%
% Description:        Returns Table 2 and 5 Appendix and stores data for
%                     best response Figures
%
% Language:           MATLAB R2013b (8.2.0.701) 64 Bit
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear
path='C:/Users/mrueda/Documents/MATLAB/Party_delegates/R&R/replication/';

%Importing data
data=strcat(path,'panel_matlab.xls');
[data2012,~]=xlsread(data,'2012');
[data2009,labels]=xlsread(data,'2009');
data=[data2009;data2012];
%data=data2012;

d_s=dataset({data(:,:) labels{:}});

%First stage vars
varsx={'l_d_RC_others' 'l_margin' 'lcasillas' 'school_ic' 'l_diff_PRI_PAN' 'lpop' 'l_turnout' 'local' 'ldist_city' ...
'l_d_RC_others_sq' 'l_margin_sq' 'lcasillas_sq' 'school_ic_sq' 'l_diff_PRI_PAN_sq' 'lpop_sq' 'l_turnout_sq' 'ldist_city_sq' ...
'l_d_RC_others_l_margin' 'l_d_RC_others_lcasillas' 'l_d_RC_others_school_ic' 'l_d_RC_others_l_diff_PRI_PAN' 'l_d_RC_others_lpop' 'l_d_RC_others_l_turnout' 'l_d_RC_others_local' 'l_d_RC_others_ldist_city' ...
'l_margin_lcasillas' 'l_margin_school_ic' 'l_margin_l_diff_PRI_PAN' 'l_margin_lpop' 'l_margin_l_turnout' 'l_margin_local' 'l_margin_ldist_city' ...
'lcasillas_school_ic' 'lcasillas_l_diff_PRI_PAN' 'lcasillas_lpop' 'lcasillas_l_turnout' 'lcasillas_local' 'lcasillas_ldist_city' ...
'school_ic_l_diff_PRI_PAN' 'school_ic_lpop' 'school_ic_l_turnout' 'school_ic_local' 'school_ic_ldist_city' ...
'l_diff_PRI_PAN_lpop' 'l_diff_PRI_PAN_l_turnout' 'l_diff_PRI_PAN_local' 'l_diff_PRI_PAN_ldist_city' ...
'lpop_l_turnout' 'lpop_local' 'lpop_ldist_city' ...
'l_turnout_local' 'l_turnout_ldist_city' ...
'local_ldist_city' 'ldist_pri' 'gov_PRI' 'ldist_pan' 'gov_PAN'};

varsy={'d_RC_PRI_c' 'd_RC_PAN_c'};
varcl={'state_district'};
varcl2={'id_state'};
[Y,X,CLv]=set_design_mat(d_s,varsy,varsx,[varcl varcl2]);


%States where there is enough variation in dep. var to estimate first stage separately for each party
    states_PRI=[9 11 12 14 17 19 20 22 24 26]';
    states_PAN=[5 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 26 27 28 30]';

    CLv_PRI=CLv(:,2);
    CLv_PRI(ismember(CLv_PRI(:,1),setdiff(CLv_PRI(:,1),states_PRI)),1)=1;
    CLv_PAN=CLv(:,2);
    CLv_PAN(ismember(CLv_PAN(:,1),setdiff(CLv_PAN(:,1),states_PAN)),1)=1;
%CLv_PRI and CL_vPAN have state codes for those states where the first stage can be estimated (for each party) and a 1 for all others 


%%Table 2%%
[beta,hessian,phats,LL,auxLL,Llong]=representatives_game(Y,X,0,CLv_PAN,CLv_PRI);

%Saving first stage results and best responses
results_fit=fit_first_step(X,phats,beta(:));
export(results_fit,'File',strcat(path,'results_fitc.txt'),'delimiter',',')
%Saving LL, auxLL, Llong for Vuong and BIC
save(strcat(path,'2playerc.mat'));

%Bootstrap
nReps=500;
[betas_monte_extended,se,pvals,~,~]=bootstrap_game(Y,X,beta,nReps,0,CLv_PAN,CLv_PRI,CLv(:,1));
ses=reshape(se,[length(se)/4,4]);
pvals=reshape(pvals,[length(pvals)/4,4]);
labels={'l_d_RC_others' 'l_margin' 'lcasillas' 'school_ic' 'l_diff_PRI_PAN' 'lpop' 'l_turnout' 'local' 'ldist_city'};
results=table_game(beta,ses,labels);

%Saving bootstrap estimates for figures
export(dataset(betas_monte_extended),'File',strcat(path,'boot_extended2_sc.txt'),'delimiter',',')

%Figure PAN's and PRI's best responses to changes in prob high
var=12; range_var=(0.1:0.05:0.9)';
pri_pan=0;
[mean_X_PRI,mean_X_PAN]=marginal_design(Y,X,beta,var,range_var,pri_pan);
%High
[UCI1pan,LCI1pan,pred1pan]=marginal(beta,betas_monte_extended,mean_X_PRI,mean_X_PAN,pri_pan,0);
%Medium 
[UCI2pan,LCI2pan,pred2pan]=marginal(beta,betas_monte_extended,mean_X_PRI,mean_X_PAN,pri_pan,1);
pri_pan=1;
[mean_X_PRI,mean_X_PAN]=marginal_design(Y,X,beta,var,range_var,pri_pan);
%High
[UCI1pri,LCI1pri,pred1pri]=marginal(beta,betas_monte_extended,mean_X_PRI,mean_X_PAN,pri_pan,0);
%Medium 
[UCI2pri,LCI2pri,pred2pri]=marginal(beta,betas_monte_extended,mean_X_PRI,mean_X_PAN,pri_pan,1);
graph=[range_var LCI1pan pred1pan UCI1pan LCI2pan pred2pan UCI2pan LCI1pri pred1pri UCI1pri LCI2pri pred2pri UCI2pri];
labels_graph={'range' 'LCI1pan_h' 'pred1pan_h' 'UCI1pan_h' 'LCI2pan_h' 'pred2pan_h' 'UCI2pan_h' 'LCI1pri_h' 'pred1pri_h' 'UCI1pri_h' 'LCI2pri_h' 'pred2pri_h' 'UCI2pri_h'};
graph_br_h=dataset({graph(:,:) labels_graph{:}});
  
%Figure PAN's and PRI's best responses to changes in prob medium
var=13; range_var=(0.1:0.025:0.4)';
pri_pan=0;
[mean_X_PRI,mean_X_PAN]=marginal_design(Y,X,beta,var,range_var,pri_pan);
%High
[UCI1pan,LCI1pan,pred1pan]=marginal(beta,betas_monte_extended,mean_X_PRI,mean_X_PAN,pri_pan,0);
%Medium 
[UCI2pan,LCI2pan,pred2pan]=marginal(beta,betas_monte_extended,mean_X_PRI,mean_X_PAN,pri_pan,1);
pri_pan=1;
[mean_X_PRI,mean_X_PAN]=marginal_design(Y,X,beta,var,range_var,pri_pan);
%High
[UCI1pri,LCI1pri,pred1pri]=marginal(beta,betas_monte_extended,mean_X_PRI,mean_X_PAN,pri_pan,0);
%Medium 
[UCI2pri,LCI2pri,pred2pri]=marginal(beta,betas_monte_extended,mean_X_PRI,mean_X_PAN,pri_pan,1);
graph=[range_var LCI1pan pred1pan UCI1pan LCI2pan pred2pan UCI2pan LCI1pri pred1pri UCI1pri LCI2pri pred2pri UCI2pri];
labels_graph={'range' 'LCI1pan_m' 'pred1pan_m' 'UCI1pan_m' 'LCI2pan_m' 'pred2pan_m' 'UCI2pan_m' 'LCI1pri_m' 'pred1pri_m' 'UCI1pri_m' 'LCI2pri_m' 'pred2pri_m' 'UCI2pri_m'};
graph_br_m=dataset({graph(:,:) labels_graph{:}});

export(graph_br_h,'XLSfile',strcat(path,'pred_best_response_h2d.xls'))
export(graph_br_m,'XLSfile',strcat(path,'pred_best_response_m2d.xls'))


%%Table 5%%
beta_ls=representatives_game_LS(Y,X,0,CLv_PAN,CLv_PRI);
%Minimum not found when using OLS as prior. Second iteration 
beta_ls=representatives_game_LS(Y,X,0,CLv_PAN,CLv_PRI,beta_ls(:));

nReps=500;
[betas_ls,ses_ls,pvals_ls,~,~]=bootstrap_game_LS(Y,X,beta_ls,nReps,0,CLv_PAN,CLv_PRI,CLv(:,1));
ses_ls=reshape(ses_ls,[length(ses_ls)/4,4]);
results_ls=table_game(beta_ls,ses_ls,labels);

%Figure PAN's and PRI's best responses to changes in prob high
var=12; range_var=(0.1:0.05:0.9)';
pri_pan=0;
[mean_X_PRI,mean_X_PAN]=marginal_design(Y,X,beta_ls,var,range_var,pri_pan);
%High
[UCI1pan,LCI1pan,pred1pan]=marginal(beta_ls,betas_ls,mean_X_PRI,mean_X_PAN,pri_pan,0);
%Medium 
[UCI2pan,LCI2pan,pred2pan]=marginal(beta_ls,betas_ls,mean_X_PRI,mean_X_PAN,pri_pan,1);
pri_pan=1;
[mean_X_PRI,mean_X_PAN]=marginal_design(Y,X,beta_ls,var,range_var,pri_pan);
%High
[UCI1pri,LCI1pri,pred1pri]=marginal(beta_ls,betas_ls,mean_X_PRI,mean_X_PAN,pri_pan,0);
%Medium 
[UCI2pri,LCI2pri,pred2pri]=marginal(beta_ls,betas_ls,mean_X_PRI,mean_X_PAN,pri_pan,1);
graph=[range_var LCI1pan pred1pan UCI1pan LCI2pan pred2pan UCI2pan LCI1pri pred1pri UCI1pri LCI2pri pred2pri UCI2pri];
labels_graph={'range' 'LCI1pan_h' 'pred1pan_h' 'UCI1pan_h' 'LCI2pan_h' 'pred2pan_h' 'UCI2pan_h' 'LCI1pri_h' 'pred1pri_h' 'UCI1pri_h' 'LCI2pri_h' 'pred2pri_h' 'UCI2pri_h'};
graph_br_h=dataset({graph(:,:) labels_graph{:}});
  
%Figure PAN's and PRI's best responses to changes in prob medium
var=13; range_var=(0.1:0.025:0.4)';
pri_pan=0;
[mean_X_PRI,mean_X_PAN]=marginal_design(Y,X,beta_ls,var,range_var,pri_pan);
%High
[UCI1pan,LCI1pan,pred1pan]=marginal(beta_ls,betas_ls,mean_X_PRI,mean_X_PAN,pri_pan,0);
%Medium 
[UCI2pan,LCI2pan,pred2pan]=marginal(beta_ls,betas_ls,mean_X_PRI,mean_X_PAN,pri_pan,1);
pri_pan=1;
[mean_X_PRI,mean_X_PAN]=marginal_design(Y,X,beta_ls,var,range_var,pri_pan);
%High
[UCI1pri,LCI1pri,pred1pri]=marginal(beta_ls,betas_ls,mean_X_PRI,mean_X_PAN,pri_pan,0);
%Medium 
[UCI2pri,LCI2pri,pred2pri]=marginal(beta_ls,betas_ls,mean_X_PRI,mean_X_PAN,pri_pan,1);
graph=[range_var LCI1pan pred1pan UCI1pan LCI2pan pred2pan UCI2pan LCI1pri pred1pri UCI1pri LCI2pri pred2pri UCI2pri];
labels_graph={'range' 'LCI1pan_m' 'pred1pan_m' 'UCI1pan_m' 'LCI2pan_m' 'pred2pan_m' 'UCI2pan_m' 'LCI1pri_m' 'pred1pri_m' 'UCI1pri_m' 'LCI2pri_m' 'pred2pri_m' 'UCI2pri_m'};
graph_br_m=dataset({graph(:,:) labels_graph{:}});

export(graph_br_h,'XLSfile',strcat(path,'pred_best_response_h2lsb2.xls'))
export(graph_br_m,'XLSfile',strcat(path,'pred_best_response_m2lsbc2.xls'))

%Saving first stage results and best responses
results_fit_ls=fit_first_step(X,phats,beta_ls(:));
export(results_fit_ls,'File',strcat(path,'results_fit_lsb.txt'),'delimiter',',')

%Table 2
results
%Table 5
results_ls
